<?php

function reequipment_adminapi_createitems($args)
{
    extract($args);

    // Argument check - make sure that all required arguments are present,
    // if not then set an appropriate error message and return
    $invalid = array();
    if (!isset($part_number) || !is_string($part_number)) {
        $invalid[] = 'part_number';
    } 
    if (!isset($company_id) || !is_int($company_id)) {
        $invalid[] = 'company_id';
    } 
    if (!isset($man_part_number) || !is_string($man_part_number)) {
        $invalid[] = 'man_part_number';
    } 
    if (!isset($cost) || !is_float($cost)) {
        $invalid[] = 'cost';
    } 
    if (count($invalid) > 0) {
        $msg = xarML('Invalid #(1)', join(', ', $invalid));
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    } 
    
    // Need to get the model id.
    if ($equipment_id != '0'){
		$model = xarModAPIFunc('reequipment', 'user', 'getequipment', array('equipment_id' => $equipment_id));
		foreach ($model as $var){
			$model_id = $var['model_id'];
			$cycles = $var['cycles'];
		}
	} else {
		$model_id = '0';
		$cycles = '0';
	}
    
    // Assume that when this is entered is when it was ordered.
    $last_ordered = time();
   
    /* Fields in table.
    item_id
	part_number
	description
	model_id
	equipment_id                                                                                                       
	company_id                                                                                                           
	last_ordered                   
	man_part_number
	cost
   */
    
    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reitems'];
    // Get next ID in table
    $nextId = $dbconn->GenId($table);

    // Add item
    $query = "INSERT INTO $table ("
        . " item_id, part_number, invoice_number, description, model_id, equipment_id, company_id, last_ordered, man_part_number, cost)"
        . " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $result =& $dbconn->Execute($query, array($nextId, $part_number, $invoice_number, $description, $model_id, $equipment_id, $company_id, $last_ordered, $man_part_number, $cost));
    if (!$result) return;
    // Get the ID of the item that we inserted
    $id = $dbconn->PO_Insert_ID($table, 'item_id');
    
    // Need to record the cycles from this piece of equipment for later reporting and analysis
    if ($equipment_id != '0'){
    	$cycles = xarModAPIFunc('reequipment', 'admin', 'createrelcycles', array('equipment_id' => $equipment_id, 'item_id' => $id));
    }

    // Let any hooks know that we have created a new forum
    $args['module'] = 'reequipment';
    $args['itemtype'] = 5; // post
    $args['itemid'] = $id; // post
    xarModCallHooks('item', 'create', $id, $args);// post
    // Return the id of the newly created link to the calling process
    return $id;
}
?>